/**
 * 
 */
package tests;

import static org.junit.Assert.*;

import java.util.List;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import forum.Member;

import server.controller.Controller;

/**
 * @author Olga
 * 
 */
public class StorageTest {

	Controller controller;

	/**
	 * @throws java.lang.Exception
	 */
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	/**
	 * @throws java.lang.Exception
	 */
	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		this.controller = new Controller();
		this.controller.login("Olgasib", "1234");
		this.controller.login("Masha", "gxyze");
	}

	/**
	 * @throws java.lang.Exception
	 */
	@After
	public void tearDown() throws Exception {
	}

/*	*//**
	 * Test method for
	 * {@link controller.Storage#login(java.lang.String, java.lang.String)}.
	 *//*
	@Test
	public void testLogin() {
		// fail("Not yet implemented");
	}

	*//**
	 * Test method for {@link controller.Storage#loggoff(java.lang.String)}.
	 *//*
	@Test
	public void testLoggoff() {
		// fail("Not yet implemented");
	}

	*//**
	 * Test method for
	 * {@link controller.Storage#removeUser(java.lang.String, java.lang.String)}.
	 *//*
	@Test
	public void testRemoveUser() {
		// fail("Not yet implemented");
	}

	*//**
	 * Test method for
	 * {@link controller.Storage#addUser(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, char)}
	 * .
	 *//*
	@Test
	public void testAddUser() {
		// fail("Not yet implemented");
	}

	*//**
	 * Test method for
	 * {@link controller.Storage#addUserFriend(java.lang.String, java.lang.String)}
	 * .
	 *//*
	@Test
	public void testAddUserFriend() {
		// fail("Not yet implemented");
	}

	*//**
	 * Test method for
	 * {@link controller.Storage#removeUserFriend(java.lang.String, java.lang.String)}
	 * .
	 *//*
	@Test
	public void testRemoveUserFriend() {
		// fail("Not yet implemented");
	}

	*//**
	 * Test method for
	 * {@link controller.Storage#addForum(java.lang.String, java.lang.String)}.
	 *//*
	@Test
	public void testAddForum() {
		
		// logged in moderator adds Forum
		assertEquals(0, this.storage.addForum("ForumSubject0", "Olgasib"));
		// forum is already exist
		assertEquals(-1, this.storage.addForum("ForumSubject0", "Olgasib"));
		// moderator is not logged in
		assertEquals(-2, this.storage.addForum("ForumSubject0", "Alonsgl"));

		this.storage.addUser("notModerator", "fName", "lName", "pw", "mail",
				'g');
		this.storage.login("notModerator", "pw");
		// not moderator trying to add forum
		assertEquals(-3, this.storage.addForum("ForumSubject0", "notModerator"));
		// not registered user adds forum
		assertEquals(-4,
				this.storage.addForum("ForumSubject0", "notRegistered"));
		// logged in moderator adds forum with empty subject
		assertEquals(-5, this.storage.addForum("", "Masha"));
		// logged in moderator adds forum with null subject
		assertEquals(-5, this.storage.addForum(null, "Masha"));
		// one more moderator adds one more forum
		assertEquals(0, this.storage.addForum("ForumSubject1", "Masha"));
		assertEquals(2, this.storage.getForums().getForums().size());
		
	}

	*//**
	 * Test method for
	 * {@link controller.Storage#addThread(int, java.lang.String, java.lang.String, java.lang.String)}
	 * .
	 *//*
	@Test
	public void testAddThread() {
		
		// fail("Not yet implemented");
		this.storage.addForum("ForumSubject0", "Olgasib");
		this.storage.addForum("ForumSubject1", "Masha");
		// logged in moderator adding the thread
		assertEquals(0, this.storage.addThread(0, "subject0ForForum0",
				"content", "Olgasib"));
		// logged in moderator adds existing thread
		assertEquals(-1, this.storage.addThread(0, "subject0ForForum0",
				"content", "Masha"));		
		// logged off moderator tried to add thread
		assertEquals(-2,
				this.storage.addThread(0, "subject", "content", "Alonsgl"));

		this.storage.addUser("notModerator", "fName", "lName", "pw", "mail",
				'g');
		this.storage.login("notModerator", "pw");
		// simple user adds thread
		assertEquals(0, this.storage.addThread(0, "subject1ForForum0",
				"content", "notModerator"));
		// simple user adds thread that already exist
		assertEquals(-1, this.storage.addThread(0, "subject0ForForum0",
				"content", "notModerator"));
		// not registered user adds thread
		assertEquals(-3, this.storage.addThread(0, "subject2ForForum0",
				"content", "notRegistered"));
		// simple user adds thread
		assertEquals(0, this.storage.addThread(1, "subject2ForForum1",
				"content", "notModerator"));
		// add thread to forumID, which does not exist
		assertEquals(-1, this.storage.addThread(5, "subject2ForForum1",
				"content", "notModerator"));
		// tread with empty subject
		assertEquals(-4, this.storage.addThread(1, "", "content", "Olgasib"));
		// tread with null subject
		assertEquals(-4, this.storage.addThread(0, null, "content", "Olgasib"));
		assertEquals(2, this.storage.getForums().getForum(0).getThreads()
				.size());		

	}

	*//**
	 * Test method for
	 * {@link controller.Storage#addPost(int, int, int, java.lang.String, java.lang.String, java.lang.String)}
	 * .
	 *//*
	@Test
	public void testAddPost() {

		// fail("Not yet implemented");
		this.storage.addForum("ForumSubject0", "Olgasib");
		this.storage.addThread(0, "subject0ForForum0", "content", "Olgasib");
		// logged in moderator adds post
		assertEquals(0, this.storage.addPost(0, 0, "postSubject0",
				"content", "Olgasib"));
		// logged in moderator tries to add double reply
		assertEquals(-1, this.storage.addPost(0, 0, "postSubject0",
				"content", "Olgasib"));

		assertEquals(0, this.storage.addPost(0, 0, "postSubject0",
				"content", "Masha"));
		assertEquals(0, this.storage.addPost(0, 1, "postSubject0",
				"content", "Olgasib"));
		// logged off moderator adds post
		assertEquals(-2, this.storage.addPost(0, 1, "postSubject0",
				"content", "Alonsgl"));
		// not registered user add post
		assertEquals(-3, this.storage.addPost(0, 1, "postSubject0",
				"content", "notRegistered"));
		// empty subject
		assertEquals(-4,
				this.storage.addPost(0, 1, "", "content", "Olgasib"));
		// null subject
		assertEquals(-4,
				this.storage.addPost(0, 1, null, "content", "Olgasib"));
		assertEquals(2, this.storage.getForums().getForum(0).getThread(0)
				.getPost(0).getReplies().size());
				
		this.storage.addForum("subject0", "Olgasib");
		this.storage.addThread(0, "thread0", "c0", "Olgasib");
		assertEquals(0, this.storage.addPost(0, 0, "post1", "content", "Masha"));
		assertEquals(0, this.storage.addPost(0, 0, "post2", "content", "Olgasib"));
		assertEquals(0, this.storage.addPost(0, 1, "post3", "content", "Masha"));
		assertEquals(0, this.storage.addPost(0, 2, "post4", "content", "Masha"));

		this.storage.addUser("notModerator", "fName", "lName", "pw", "mail",
		'g');
		this.storage.login("notModerator", "pw");
		assertEquals(0, this.storage.addPost(0, 4, "post5", "content", "notModerator"));		
		assertEquals(0, this.storage.addPost(0, 5, "post6", "content", "notModerator"));
		assertEquals(0, this.storage.addPost(0, 6, "post7", "content", "notModerator"));

	}

	*//**
	 * Test method for
	 * {@link controller.Storage#removePost(java.lang.String, int, int, int)}.
	 *//*
	@Test
	public void testRemovePost() {

		// fail("Not yet implemented");
		this.storage.addForum("subject0", "Olgasib");
		this.storage.addThread(0, "thread0", "c0", "Olgasib");
	    this.storage.addPost(0, 0, "post1", "content", "Masha");
		this.storage.addPost(0, 0, "post2", "content", "Olgasib");
		this.storage.addPost(0, 1, "post3", "content", "Masha");
		this.storage.addPost(0, 2, "post4", "content", "Masha");
		//System.out.println(this.storage.getForums().getForum(0).getThread(0).showAllTread());

		this.storage.addUser("notModerator", "fName", "lName", "pw", "mail",
		'g');
		this.storage.login("notModerator", "pw");
		this.storage.addPost(0, 4, "post5", "content", "notModerator");		
		this.storage.addPost(0, 5, "post6", "content", "notModerator");
		this.storage.addPost(0, 6, "post7", "content", "notModerator");
		
	//	System.out.println(this.storage.getThreadsOfTopic("Masha", 0, 0));
		this.storage.addForum("subject1", "Olgasib");
		this.storage.addThread(1, "thread1", "c1", "Olgasib");
		this.storage.addPost(1, 8, "post9", "content", "Masha");
	//	System.out.println(this.storage.getForums().showAllForums());
		
		assertEquals(0, this.storage.removePost("Olgasib", 0, 3));
		assertEquals(0, this.storage.removePost("notModerator", 0, 6));
		assertEquals(-1, this.storage.removePost("Alonsgl", 0, 1));
		assertEquals(-2, this.storage.removePost("notRegistered", 0, 1));
		assertEquals(-2, this.storage.removePost("notModerator", 0, 1));
		assertEquals(-3, this.storage.removePost("Olgasib", 0, 6));
		assertEquals(-3, this.storage.removePost("Masha", 0, 8));
		assertEquals(-3, this.storage.removePost("notModerator", 0, 8));
		assertEquals(0, this.storage.removePost("Masha", 1, 8));		
		assertEquals(-3, this.storage.removePost("Masha", 1, 9));
		assertEquals(-5, this.storage.removePost("Masha", 33, 2));
	}

	*//**
	 * Test method for
	 * {@link controller.Storage#removeThread(java.lang.String, int, int)}.
	 *//*
	@Test
	public void testRemoveThread() {
		
		this.storage.addForum("subject0", "Olgasib");
		this.storage.addThread(0, "thread0", "c0", "Olgasib");
		this.storage.addThread(0, "thread1", "c0", "Masha");
		this.storage.addUser("notModerator", "fName", "lName", "pw", "mail",
				'g');
		this.storage.login("notModerator", "pw");
		this.storage.addThread(0, "thread2", "c0", "notModerator");
		this.storage.addThread(0, "thread3", "c0", "notModerator");
		this.storage.addThread(0, "thread4", "c0", "notModerator");
		this.storage.addThread(0, "thread5", "c0", "notModerator");
		//moderator removes thread that was published by another moderator
		assertEquals(0, this.storage.removeThread("Olgasib", 0, 1));
		//moderator removes thread that was published by not moderator
		assertEquals(0, this.storage.removeThread("Olgasib", 0, 4));
		//user removes thread that was published by user
		assertEquals(0, this.storage.removeThread("notModerator", 0, 5));
		assertEquals(3, this.storage.getForums().getForum(0).getThreads().size());
		//logged out removes thread
		assertEquals(-1, this.storage.removeThread("Alonsgl", 0, 0));
		//not registered removes thread
		assertEquals(-2, this.storage.removeThread("notRegistered", 0, 0));
		//not moderator removes thread that was published by another user
		assertEquals(-2, this.storage.removeThread("notModerator", 0, 0));
		//moderator removes thread that does not exist
		assertEquals(-3, this.storage.removeThread("Olgasib", 0, 1));
		//moderator removes thread from forum that does not exist
		assertEquals(-4, this.storage.removeThread("Masha", 6, 1));
		assertEquals(3, this.storage.getForums().getForum(0).getThreads().size());
		
	}

	/**
	 * Test method for
	 * {@link controller.Storage#removeForum(java.lang.String, int)}.
	 *//*
	@Test
	public void testRemoveForum() {
		
		int res = 1;
		res &= this.storage.addForum("subject0", "Olgasib");
		res &= this.storage.addForum("subject1", "Masha");
		res &= this.storage.addForum("subject2", "Olgasib");
		res &= this.storage.addForum("subject3", "Masha");
		res &= this.storage.addForum("subject4", "Olgasib");
		assertEquals(res, 1);
		// logged in moderator removes forum, that was added by other moderator
		assertEquals(0, this.storage.removeForum("Masha", 2));
		// logged of moderator removes forum, that was added by other moderator
		assertEquals(-1, this.storage.removeForum("Alonsgl", 0));
		// logged user removes forum, that was added by other moderator
		this.storage.registerUser("notModerator", "fName", "lName", "pw", "mail",
				'm');
		this.storage.login("notModerator", "pw");
		assertEquals(-2, this.storage.removeForum("notModerator", 0));
		assertEquals(-2, this.storage.removeForum("notRegistered", 0));
		//moderator removes not-existing forum
		assertEquals(-3, this.storage.removeForum("Olgasib", 2));
		assertEquals(-3, this.storage.removeForum("Olgasib", 7));
		assertEquals(4, this.storage.getForums().size());


	}

	@Test
	public void testEditPost() {
		this.storage.addForum("subject0", "Olgasib");
		this.storage.addThread(0, "thread0", "c0", "Olgasib"); //0
		
	    this.storage.addPost(0, 0, "post1", "content", "Masha"); //1
		this.storage.addPost(0, 0, "post2", "content", "Olgasib"); //2
		this.storage.addPost(0, 1, "post3", "content", "Masha"); //3
		this.storage.addPost(0, 2, "post4", "content", "Masha"); //4
		//System.out.println(this.storage.getForums().getForum(0).getThread(0).showAllTread());

		this.storage.addUser("notModerator", "fName", "lName", "pw", "mail",
		'g');
		this.storage.login("notModerator", "pw");
		this.storage.addPost(0, 4, "post5", "content", "notModerator"); //5		
		this.storage.addPost(0, 5, "post6", "content", "notModerator"); //6
		this.storage.addPost(0, 6, "post7", "content", "notModerator"); //7
		
	//	System.out.println(this.storage.getThreadsOfTopic("Masha", 0, 0));
		this.storage.addForum("subject1", "Olgasib");
		this.storage.addThread(1, "thread1", "c1", "Olgasib"); //8
		this.storage.addPost(1, 8, "post9", "content", "Masha"); //9
		this.storage.addPost(0, 0, "post1", "content1", "Masha"); //10
		
		assertEquals(0, this.storage.editPost("Masha", 0, 3, "new content"));
		//user has no premitions to edit this post
		assertEquals(-1, this.storage.editPost("Olgasib", 0, 3, "new content"));
		assertEquals(-1, this.storage.editPost("Olgasib", 1, 9, "new content"));
		//post does not exist
		assertEquals(-1, this.storage.editPost("Olgasib", 0, 10, "new content"));
		//post does not exist
		assertEquals(-1, this.storage.editPost("Olgasib", 4, 8, "new content"));
		//post with such content is already exist
		assertEquals(-1, this.storage.editPost("Masha", 0, 10, "content"));
		//user must be login, to edit post
		assertEquals(-2, this.storage.editPost("Alonsgl", 0, 3, "new content"));
		//user must be login, to edit post
		assertEquals(-3, this.storage.editPost("notRegisteredUser", 0, 3, "new content"));
		//new content cannot be empty
		assertEquals(-4, this.storage.editPost("Olgasib", 0, 3, ""));
		//new content cannot be null
		assertEquals(-4, this.storage.editPost("Olgasib", 0, 3, null));

	
	}
	

	*//**
	 * Test method for {@link controller.Storage#getListOfForums()}.
	 *//*
	@Test
	public void testGetListOfForums() {
		// fail("Not yet implemented");
	}

	*//**
	 * Test method for {@link controller.Storage#getThreadsTopicsOfForum(int)}.
	 *//*
	@Test
	public void testGetThreadsTopicsOfForum() {
		// fail("Not yet implemented");
	}

	*//**
	 * Test method for {@link server.controller.Controller#getThreadsOfTopic(int)}.
	 *//*
	@Test
	public void testGetThreadsOfTopic() {
		// fail("Not yet implemented");
	}*/
	
	@Test
	public void testGetFriendsOfUser() {
		this.controller.registerUser("testuser", "fName", "lName","pw", "mail", 'm');
		this.controller.registerUser("testuser1", "fName1", "lName1","pw1", "mail1", 'f');
		this.controller.registerUser("testuser2", "fName2", "lName2","pw2", "mail2", 'm');
		this.controller.registerUser("testuser3", "fName3", "lName3","pw3", "mail3", 'm');
		this.controller.login("testuser", "pw");
		this.controller.addUserFriend("testuser", "testuser1");
		this.controller.addUserFriend("testuser", "testuser2");
		this.controller.addUserFriend("testuser", "testuser3");
		String ans = "";
		List<Member> friends = this.controller.getFriendsOfUser("testuser");
		for (int i = 0; i < friends.size(); i++)
            ans += "friend:"+friends.get(i).getUsername()+"\n";
        ans+="\0";
		assertFalse(ans.isEmpty());
		assertTrue(ans.contains("testuser1"));
		assertTrue(ans.contains("testuser2"));
		assertTrue(ans.contains("testuser3"));
	}

}
